<?php
namespace Home\Controller;
use Think\Controller;
class ShopController extends Controller {
    public function index(){

  		$this->display(); 
    }

    public function registe(){

    	$this->display();  

    }

    public function service(){

    	$this->display();  

    }


    public function addshop(){
        //判断用户是否登录没有登录跳转登录页面
        if($_SESSION['login']!=1 || !$_SESSION['username']){
            $this->error('您要增添商铺必须先登录，马上为您跳转到登陆页面!',U('User/index'));
            

        }else{
            $this->display(); 
        }

        
    }

     public function shopAdd(){
        //判断用户是否登录没有登录跳转登录页面
        // var_dump(I('shop_telephone'));


        $shop = D("shop"); // 实例化User对象
        // var_dump($_POST);
        // $_POST['user_password']=md5(I('post.user_password'));
        // $_POST['repassword']=md5(I('post.repassword'));
        if (!$shop->create()){
             // 如果创建失败 表示验证没有通过 输出错误提示信息

            // echo"<pre>";
            // print_r($user->getError());
            // echo "</pre>";
            $this->ajaxReturn($shop->getError());
        }else{
             // 验证通过 可以进行其他数据操作
            $shop->shop_integral_grade=1;
            $shop->shop_consumption_integral=0;
            $shop->shop_credit_rating=1;
            $shop->shop_credit_score=0;
            $shop->shop_creat_user_id=$_SESSION['userid'];
            $shop->shop_creat_user_name=$_SESSION['username'];
            $shop->shop_is_open=1;
            $shop->shop_is_bookable=0;
            $shop->shop_is_groupon=0;
            $shop->shop_remark_num=0;
            $shop->shop_remark_best_num=0;
            $shop->shop_remark_good_num=0;
            $shop->shop_remark_bad_num=0;
            $shop->shop_becollected_num=0;
            $shop->shop_introduction=I('post.content');




            $shop->shop_creat_time=date("Y-m-d H:i:s" ,time());
            $shop->shop_last_modify_time=$shop->shop_creat_time;
            if($shop->add()){

                //增加用户积分
                $user = A('User');
                // 调用 User 模块中的方法
                $user->add_user_consumption_integral(50);      

                $this->success('添加成功,我们和商铺都会对您不胜感激，给你增加积分略表谢意！',U('Index/index'),2);

                 // 将增加的店铺id传到跳转页面
                // $rstshop=M('shop');
                // $shop_name=I('shop_name');
                // $rstshopid=$rstshop->where("shop_name=$shop_name")->getField('id');


                //   // var_dump($rstshopid);


                // $this->success('添加成功,我们和商铺都会对您不胜感激，给你增加积分略表谢意！',U('Shop/shop',array('id'=>$rstshopid)),2);
                // // U('mod',array('id'=>$arr)))
            }
        }

    }
    


     public function shop(){
        // $shopid=I('id');
        // $shopid=I('shop');//商铺Id
        $shopid=I('get.id');//商铺Id
        var_dump('shop');

        var_dump($shopid);
        // $shop = M("shop"); // 实例化User对象
        // $shopinfo = $shop->where("id=$shopid")->find();

        //读取shop表信息
        $shop = M("shop"); // 实例化User对象
        $shopinfo = $shop->where("id='$shopid'")->find();
        var_dump($shopinfo);
        //读取shop_pics信息
        $shop_pics = M("shop_pics"); // 实例化User对象
        //通过shop_pic_shop_id和shop_pic_class_id读取
        $shop_pics = $shop_pics->where("shop_pic_shop_id='$shopid' AND shop_pic_class_id=1")->limit(6)->select();
        if($shop_pics){
            $ispic=1;
            $this->assign('shop_pics',$shop_pics);
        }else{
            $ispic=0;
        }
        $this->assign('ispic',$ispic);
        // var_dump($ispic);
        // var_dump($shop_pics);

// $User->where('status=1')->field('id,name')->limit(10)->select();$User->where('type=1 AND status=1')->select();   

        
        //判断是否有详细信息
        // if(isEmptyorNull($shopinfo->shop_details)){
        //     $isdetail=0;
        // }else{
        //      $isdetail=1;
        // }
        //  var_dump($isdetail);
        //  exit();


        $this->assign('isdetail',$isdetail);

        $this->assign($shopinfo);

        // $this->display('test'); 
        $this->display();  


    }
    public function shopsearch(){
         // var_dump($_POST);

        $shop_name=I('keyword');
        $shop = M("shop"); // 实例化User对象
        $shopinfo = $shop->where("shop_name='$shop_name'")->find();
        if(!$shopinfo){
             $this->error('没有你想找的内容!');

        }else
        {
            // var_dump($shopinfo);
            $id=$shopinfo['id'];
            // var_dump('2111111111111111');
            var_dump('shopsearch');
            var_dump($id);

             $this->success('找到了！',U('Shop/shop',array('id'=>$id)),1);
        }



    }
    //为商铺增加展示图片

     public function addshoppic(){
        $id=I('id');//商铺Id
        $this->assign('id',$id);

        $this->display();  

    }

    public function shoppicupload(){
        // var_dump($_GET);
        // var_dump($_POST);


        $id=I('get.id');//商铺Id
        // var_dump($id);
        // exit();

        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =     './Public/Uploads/'; // 设置附件上传根目录
        $upload->savePath='./Images/'; // 设置附件上传（子）目录



        // 上传文件 
        var_dump($upload);
        $info   =   $upload->upload();
        if(!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        }else{// 上传成功

            // $info =  $upload->getUploadFileInfo();
            echo "<pre>";
            print_r($info);
            echo "</pre>";
            // $this->success('上传成功！');

            //生成缩略图
            $thumbWidth=218;
            $thumbHeight=218;
            $upload_num=0;

            $image = new \Think\Image(); 
                foreach($info as $file) {
                    $thumb_file = './Public/Uploads/' . $file['savepath'] . $file['savename'];
                    $save_path = './Public/Uploads/' .$file['savepath'] . 'mini_' . $file['savename'];
                    $image->open( $thumb_file )->text('我爱预约','./Public/Uploads/ariblk.ttf',30,'#A7AAA4',\Think\Image::IMAGE_WATER_SOUTHWEST)->save( $thumb_file );
                    $image->open( $thumb_file )->text('我爱预约','./Public/Uploads/ariblk.ttf',24,'#A7AAA4',\Think\Image::IMAGE_WATER_SOUTHWEST)->thumb( $thumbWidth, $thumbHeight, \Think\Image::IMAGE_THUMB_FIXED)->save( $save_path );


                    //信息入库
                     // var_dump('mini_' .$file['savename']);
                    // var_dump($upload->rootPath);


                    // var_dump($file['savepath']);
                    // var_dump($file['savename']);

                    // var_dump($file['savepath'] . $file['savename']);
                    // var_dump($file['savepath'] . 'mini_' .$file['savename']);
                    $strrootpath=$upload->rootPath;
                    $strsavepath=$file['savepath'];

                    $newrootpath=substr($strrootpath,1,strlen($strrootpath)-2);
                    $newsavepath=substr($strsavepath,1,strlen($strsavepath)-1);

                    var_dump($newrootpath);
                    var_dump($newsavepath);





                    // string(20) "./Images/2015-07-14/" 
                    // string(17) "55a51868ebb02.jpg" 
                    // string(37) "./Images/2015-07-14/55a51868ebb02.jpg" string(42) "./Images/2015-07-14/mini_55a51868ebb02.jpg" 
                    $shop_pic=M('shop_pics');
                    $shop_pic->shop_pic_name='mini_' .$file['savename'];
                    //文件路径和上面设置对应
                    $shop_pic->shop_pic_path=$newrootpath.$newsavepath;
                    var_dump($shop_pic->shop_pic_path);
                    $shop_pic->shop_pic_shop_id=$id; 
                    $shop_pic->shop_pic_class_id=1;   

                    $shop_pic->shop_pic_upload_time=date("Y-m-d H:i:s" ,time());
                    $shop_pic->shop_pic_upload_user_name=$_SESSION['username'];
                    
                    if($shop_pic->add()){
                        //保存成功
                        $upload_num+=1;
                    }                   
                }

                //全部保存成功--给用户增加积分

                 //增加用户积分
                $userintegral=$upload_num*20;//每添加一个图片得到20积分
                $user = A('User');
                // 调用 User 模块中的方法
                $user->add_user_consumption_integral($userintegral);
                var_dump($id);
               

                // $this->success('添加成功,我们和商铺都会对您不胜感激，给你增加积分略表谢意！',U('Shop/shop',array('id'=>$id)),2);

                 // 将增加的店铺id传到跳转页面
                // $rstshop=M('shop');
                // $shop_name=I('shop_name');
                // $rstshopid=$rstshop->where("shop_name=$shop_name")->getField('id');


                //   // var_dump($rstshopid);


                $this->success('添加成功,我们和商铺都会对您不胜感激，给你增加积分略表谢意！',U('Shop/shop',array('id'=>$id)),2);
                // // U('mod',array('id'=>$arr))),array('id'=>$id)


            }
       }
    //为餐厅增加详细信息 

    public function addshopdetail(){
        $id=I('id');//商铺Id
        // var_dump($id);

        $this->assign('id',$id);
        $this->display();  
    }
    public function updateshopdetail(){
        $shopid=I('get.id');//商铺Id
        var_dump('shop');

        var_dump($shopid);
        // $shop = M("shop"); // 实例化User对象
        // $shopinfo = $shop->where("id=$shopid")->find();

        //tp手册，更新数据方法
    //    $User = M("User"); // 实例化User对象
    // // 要修改的数据对象属性赋值
    // $User->name = 'ThinkPHP';
    // $User->email = 'ThinkPHP@gmail.com';
    // $User->where('id=5')->save(); // 根据条件更新记录

        //读取shop表信息
        $shop = M("shop"); // 实例化User对象
        $shop->shop_details=I('post.content').'<br>(添加者：'.$_SESSION['username'].')';
        $rst=$shop->where("id='$shopid'")->save();
        if(!$rst){
             $this->error('添加失败!');


        }else{

            //给用户增加积分20
            $user = A('User');
                // 调用 User 模块中的方法
            $user->add_user_consumption_integral(20);

             $this->success('添加成功,我们和商铺都会对您不胜感激，给你增加积分略表谢意！',U('Shop/shop',array('id'=>$shopid)),2);


        }

        // $this->assign('id',$id);
        // $this->display();  
    }



     public function shopcenter(){
        $id=I('id');//商铺Id
        var_dump($id);

        $this->assign('id',$id);
        $this->display();  
    }


    public function shopcenterindex(){
        // $id=I('id');//商铺Id
        // var_dump($id);

        // $this->assign('id',$id);
        $this->display();  
    }

    public function shopServiceClass(){
        $id=I('id');//商铺Id
        var_dump('shopServiceClass中打印');
        var_dump($id);
        $this->assign('id',$id);   
        $cate=M('Shop_service_category');
            $list=$cate->where("shop_id='$id'")->field("id,name,pid,path,concat(path,'-',id) as bpath")->order('bpath')->select();
                    // var_dump($list);
                    // exit();
            foreach($list as $key=>$value){
               $list[$key]['count']=count(explode('-',$value['bpath']));
            }
            $this->assign('alist',$list);      

        // $this->assign('id',$id);
        $this->display();  
    }


  //    function index(){
  //   $cate=M('cate');
  //   $list=$cate->field("id,name,pid,path,concat(path,'-',id) as bpath")->order('bpath')->select();
  //   foreach($list as $key=>$value){
  //      $list[$key]['count']=count(explode('-',$value['bpath']));
  //   }
  //   $this->assign('alist',$list);
  //   $this->display();  
  // }
  //添加栏目
  function addShopServiceClass(){

    $shopid=I('shopid');//商铺Id
    var_dump('addShopServiceClass中打印');
    var_dump($shopid);
    // $cate=new ShopServiceCategoryModel();//3.2出错
    $cate=D('Shop_service_category');

    
    if($vo=$cate->create()){
        $cate->shop_id=$shopid;
         //初始化是叶子分类
        $cate->leafcategory=1;
        // var_dump($cate);
        // exit();
      if($cate->add()){
         //改变父分类为非叶子分类
        $pid=isset($_POST['pid'])?(int)$_POST['pid']:0;
        $pidcate=M('Shop_service_category');
        // $User-> where('id=5')->setField('name','ThinkPHP');
        if($pidcate){
             $pidcate->where("id='$pid'")->setField('leafcategory','0');
            //没找到就是根分类，就不用设置了
        }

        $this->success('添加栏目成功');  
      }else{
        $this->error('添加栏目失败');  
      }
      //dump($vo);  
    }else{
      $this->error($cate->getError());  
    }
  }

   public function tree()
    {
        $subproject = M('Shop_service_category');
        $list = $subproject->select();
        
        echo JSON_ENCODE($list);
 
    }


    public function shopServiceItem(){
        $id=I('id');//商铺Id
        var_dump('shopServiceItem中打印');
        var_dump($id);
        $this->assign('id',$id);   
        $cate=M('Shop_service_category');
            $list=$cate->where("shop_id='$id'")->field("id,name,pid,path,leafcategory,concat(path,'-',id) as bpath")->order('bpath')->select();
                    // var_dump($list);
                    // exit();
            foreach($list as $key=>$value){
               $list[$key]['count']=count(explode('-',$value['bpath']));
            }
            $this->assign('alist',$list);      

        // $this->assign('id',$id);
        $this->display();   
    }





    function addshopServiceItem(){

    $shopid=I('shopid');//商铺Id
    var_dump('shopServiceItem中打印');
    var_dump($shopid);
    // $cate=new ShopServiceCategoryModel();//3.2出错
    $cate=D('shop_service_item');

    
    if($vo=$cate->create()){
        $cate->shop_id=$shopid;
        $cate->service_item__introduction=I('post.content');
        $cate->shop_service_item_creat_user_id=$_SESSION['userid'];
        $cate->shop_service_item_creat_user_name=$_SESSION['username'];

        $cate->service_item_becreated_time=date("Y-m-d H:i:s" ,time());
        $cate->shop_service_item_last_modify_time=$cate->service_item_becreated_time;



      if($cate->add()){
       
        $this->success('添加栏目成功');  
        
      }else{
        $this->error('添加栏目失败');  
      }
      //dump($vo);  
    }else{
      $this->error($cate->getError());  
    }
  }

    public function shopmenusearch(){
        $id=I('id');//商铺Id
        var_dump($id);
        $nodeid=I('nodeid');
        var_dump($nodeid);


        // $this->assign('id',$id);
        // $this->display();  
    }










}